//
//  CustomTransition.swift
//  kxf
//
//  Created by Mac on 2024/3/17.
//

import Foundation
import UIKit

class CustomTransition: NSObject, UIViewControllerAnimatedTransitioning {
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.3 // 动画持续时间
    }
 
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        // 获取动画相关的上下文信息
        let containerView = transitionContext.containerView
        let toViewController = transitionContext.viewController(forKey: .to)
        let fromViewController = transitionContext.viewController(forKey: .from)
 
        // 添加新视图控制器的视图
        containerView.addSubview((toViewController?.view)!)
 
        // 执行动画
        toViewController?.view.alpha = 0
        UIView.animate(withDuration: self.transitionDuration(using: transitionContext), animations: {
            fromViewController?.view.alpha = 0
            toViewController?.view.alpha = 1
        }, completion: { _ in
            // 完成动画
            transitionContext.completeTransition(true)
        })
    }
}
 
